package com.example.carprovideruser.service.impl.rsh;

import com.alibaba.dubbo.config.annotation.Service;
import com.example.carinterface.pojo.InsuranceOrder;
import com.example.carinterface.pojo.Insuranceclaims;
import com.example.carinterface.pojo.OwnerCard;
import com.example.carinterface.pojo.User;
import com.example.carinterface.service.rsh.InsuranceService;

import com.example.carinterface.utils.ResultJson;
import com.example.carprovideruser.mapper.rsh.InsuranceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class InsuranceServiceImpl implements InsuranceService {
    @Autowired
    private InsuranceMapper insuranceMapper;

    /**
     * 添加保险
     *
     * @param map
     * @return
     */
    @Override
    public Boolean insertInsurance(Map<String, Object> map) {

        int i = insuranceMapper.insertInsurance(map);

        return i > 0 ? true : false;
    }

    /**
     * 根据主键查询车主卡信息
     *
     * @param id
     * @return
     */
    @Override
    public ResultJson selectOwerCardById(Integer id) {
        ResultJson resultJson = new ResultJson<>();
        List<OwnerCard> list = null;
        list = insuranceMapper.selectOwerCardById(id);
        if (list != null) {
            resultJson.getSuccess("成功", list);
            return resultJson;
        } else {
            resultJson.getFail("失败");
            return resultJson;
        }
    }

    /**
     * 根据id查询保险订单
     * @param user
     * @return
     */
    @Override
    public ResultJson selectInsuranceOrder(User user) {
        ResultJson<Object> resultJson = new ResultJson<>();
        List list = null;
        list = insuranceMapper.selectInsuranceOrder(user);
        if (list != null) {
            resultJson.getSuccess("成功", list);
            return resultJson;
        } else {
            resultJson.getFail("失败");
            return resultJson;
        }
    }

    /**
     * 核对车辆信息
     * @return
     */
    @Override
    public ResultJson checkCarInfo() {
        ResultJson<Object> resultJson = new ResultJson<>();
        Map<String,Object> map = new HashMap<>();
        List list = insuranceMapper.checkCarInfo();
        System.out.println(list);
        map.put("list",list);
        resultJson.getSuccess("成功",map);
        return resultJson;
    }

    @Override
    public ResultJson selectOfferInfo() {
        ResultJson<Object> resultJson = new ResultJson<>();
        Map<String,Object> map = new HashMap<>();
        List list = insuranceMapper.selectOfferInfo();
        System.out.println(list);
        map.put("list",list);
        resultJson.getSuccess("成功",map);
        return resultJson;
    }

    @Override
    public ResultJson getInputInfo() {
        ResultJson<Object> resultJson = new ResultJson<>();
        Map<String,Object> map = new HashMap<>();
        List list = insuranceMapper.getInputInfo();
        System.out.println(list);
        map.put("list",list);
        resultJson.getSuccess("成功",map);
        return resultJson;
    }

    @Override
    public ResultJson getStopTime(String license_Plate_Number) {
        ResultJson<Object> resultJson = new ResultJson<>();
        Map<String,Object> map = new HashMap<>();
       // List list = insuranceMapper.getStopTime(license_Plate_Number);
        List<Map> insurance = insuranceMapper.Insurance(license_Plate_Number);
        if(insurance.size()==0){
            return resultJson.setSuccess("未购买保险",1);
        }
        resultJson.setData(insurance);
        Object time = insurance.get(0).get("tian");
        int i = Integer.parseInt(time.toString());
        if(i>=0){
            //未结束。set
          return   resultJson.setSuccess("保险未结束",insurance);
        }else {
            //已结束
         return    resultJson.setFail("保险已到期，请续保");
        }

    }

    /**
     * 根据user查询保险理赔
     * @param user
     * @return
     */
    @Override
    public ResultJson getInsuranceClaims(User user) {
        ResultJson<Object> resultJson = new ResultJson<>();
        List<Map> list = insuranceMapper.getInsuranceClaims(user);
        for (Map map1 : list) {
            Integer insuranceClaimsId =  (Integer) map1.get("id");
            List insuranceClaimsInfo = insuranceMapper.getInsuranceClaimsInfo(insuranceClaimsId);
            map1.put("list",insuranceClaimsInfo);
        }

        resultJson.getSuccess("成功",list);
        return resultJson;
    }
    /*{"site":"成都",
"time":"2020-12-12",
"accidentType":1,
"locationCoordinates":"121",
"ownerCardId":5,
"tel":"231",
"state":1,
"list":[
{"carPartsPriceId":1,"damageImg":"urlimgae"},
{"carPartsPriceId":2,"damageImg":"urlimgae"},
{"carPartsPriceId":3,"damageImg":"urlimgae"}
]
}*/
    @Override
   //@Transactional
    public ResultJson insertALLInsuranceClaims(Map<String, Object> map) {
        System.out.println("---------------------------start-------------------");
       ResultJson resultJson =new ResultJson();
        Insuranceclaims insuranceclaims = new Insuranceclaims();
        insuranceclaims.setSite((String) map.get("site"));
      // insuranceclaims.setTime((Timestamp) map.get("time"));
        insuranceclaims.setAccidenttype ((Integer) map .get("accidentType"));
        insuranceclaims.setLocationcoordinates((String) map.get("locationCoordinates"));
        insuranceclaims.setownerCardId ((Integer)map.get("ownerCardId"));
        insuranceclaims.setTel ((String) map.get("tel"));
        insuranceclaims.setState ((Integer)map.get("state"));
        System.out.println("---------------------------添加insuranceclaims-------------------");
        Integer i = insuranceMapper.insertInsuranceClaims(insuranceclaims);
        Integer insuranceclaimsId = insuranceclaims.getId();
        List<Map> list = (List) map.get("list");
        Integer a = 0;
        for (Map map1: list) {
            System.out.println("\t\t"+list.size()+"============start================="+a);
            map1.put("insuranceClaimsId",insuranceclaimsId);
            Integer integer = insuranceMapper.inserCarPartsInfor(map1);
            a+=integer;
            System.out.println("\t\t"+list.size()+"==============end==============="+a);
        }
        if(a==list.size() && i==1 ){
            System.out.println("---------------------------end-------------------");
            return resultJson.setSuccess("成功");
        }
        System.out.println("---------------------------失败*******end-------------------");
        return resultJson.setFail("失败");
    }


}